package com.thoughtorigin.application {
import org.flexunit.asserts.assertEquals;
import org.flexunit.asserts.assertNotNull;
import org.flexunit.asserts.assertNull;
import org.flexunit.asserts.fail;

public class XMLApplicationSettingsTest {

    private var testxml1: XML =
            <root>
                <baseUrls>
                    <baseImageUrl>baseImageUrl</baseImageUrl>
                    <baseFeedUrl>baseFeedUrl</baseFeedUrl>
                    <baseSwfUrl>baseSwfUrl</baseSwfUrl>
                    <baseContentUrl>baseContentUrl</baseContentUrl>
                </baseUrls>
                <feeds>
                    <feed id="sampleFeed" url="item1.xml" method="post" />
                    <feed id="sampleFeed2" url="item2.xml" />
                </feeds>
                <crossDomainPolicies>
                    <policy>somethings.com/crossdomain.xml</policy>
                    <policy>somethingelse.com/crossdomain.xml</policy>
                </crossDomainPolicies>
                <additionalSettings>
                    <sample id="1" value="value" />        
                </additionalSettings>
            </root>;

    private var settings: XMLApplicationSettings;

    [Before]
    public function setup(): void
    {
        settings = new XMLApplicationSettings(testxml1);
    }

    [Test]
    public function shouldHaveADictionaryOfFeedItems(): void {
        assertNotNull(settings.feeds);
        assertEquals(testxml1.feeds.feed.(@id=="sampleFeed").@url, FeedItem(settings.feeds["sampleFeed"]).url);
        assertNull(settings.feeds["nofind"]);
    }

    [Test]
    public function shouldGetFeedItemById(): void {
        assertEquals(testxml1.feeds.feed.(@id=="sampleFeed2").@url, settings.getFeedById("sampleFeed2").url);
    }

    [Test]
    public function shouldStoreHTTPRequestMethod(): void {
        assertEquals(testxml1.feeds.feed.(@id=="sampleFeed").@method, settings.getFeedById("sampleFeed").method);
    }

    [Test]
    public function shouldHaveBaseUrls(): void {
        assertEquals(testxml1.baseUrls.baseImageUrl, settings.baseImageUrl);
        assertEquals(testxml1.baseUrls.baseFeedUrl, settings.baseFeedUrl);
        assertEquals(testxml1.baseUrls.baseSwfUrl, settings.baseSwfUrl);
        assertEquals(testxml1.baseUrls.baseContentUrl, settings.baseContentUrl);
    }

    [Test]
    public function shouldHaveListOfCrossDomainPolicies(): void
    {
        assertNotNull(settings.crossDomainPolicies);
        assertEquals(XMLList(testxml1.crossDomainPolicies.policy).length(), settings.crossDomainPolicies.length);
        assertEquals(testxml1.crossDomainPolicies.policy[0], settings.crossDomainPolicies[0])
    }

    [Test]
    public function shouldHaveMiscSettingsObject(): void {
        assertNotNull(settings.additionalProperties);
        assertEquals(testxml1.additionalSettings, settings.additionalProperties);
    }
}
}